<?php

/* * 
 * 关注
 */
namespace Api\Model;
use Think\Model;
class FriendModel extends Model {

    //添加好友
    public function addFriend($userId1, $userId2) {

        $data = array(
            array('create_time' => getCurrentDate(), 'user_id' => $userId1, 'friend_user_id' => $userId2),
            array('create_time' => getCurrentDate(), 'user_id' => $userId2, 'friend_user_id' => $userId1)
        );

        $this->addAll($data);
    }

    //删除好友
    public function deleteFriend($userId1, $userId2) {

        $myMap['user_id'] = $userId1;
        $myMap['friend_user_id'] = $userId2;

        $this->where($myMap)->delete();

        $friendMap['user_id'] = $userId2;
        $friendMap['friend_user_id'] = $userId1;

        $this->where($myMap)->delete();

    }

    //获取好友列表
    public function getFriendListByUserId($userId, $pageNum) {

        $map['f.user_id'] = $userId;
        
        $list = $this->alias('f')
            ->join("LEFT JOIN __USER__ u ON f.friend_user_id = u.user_id")
            ->field("u.user_id, u.username, u.head_image_url, u.city")
            ->where($map)
            ->order("f.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        if(isNullOrEmpty($list)) {
            $list = array();
        }

        return $list;

    }

    //判断是否为好友
    public function checkFriendRelationship($userId1, $userId2) {

        //判断是否为好友
        $friendMap['user_id'] = $userId1;
        $friendMap['friend_user_id'] = $userId2;
        $friend = D('friend')->where($friendMap)->find();

        if(!isNullOrEmpty($friend)) {
            return true;
        } else {
            return false;
        }

    }

    //根据挑战ID获取好友列表（5条头像记录）
    public function getFriendListBasicByChallengeId($challengeId, $userId) {

        $sql = "SELECT u.user_id, u.head_image_url
                FROM ff_friend f
                INNER JOIN ff_challenge_join cj ON f.friend_user_id = cj.user_id
                LEFT JOIN ff_user u ON f.friend_user_id = u.user_id
                WHERE f.user_id = " .$userId. " AND cj.challenge_id = " .$challengeId. "
                ORDER BY cj.collect_count DESC
                LIMIT 5";

        $list = M()->query($sql);

        return $list;

    }

    //根据挑战ID获取好友列表
    public function getFriendListByChallengeId($challengeId, $userId, $pageNum) {

        $sql = "SELECT cj.join_id, u.user_id, u.username, u.head_image_url, u.city, 
                (SELECT COUNT(1) + 1 FROM ff_challenge_join chj
                WHERE chj.challenge_id = cj.challenge_id
                AND chj.collect_count > cj.collect_count) AS rank  /*分数相同,名次并列*/
                FROM ff_friend f
                INNER JOIN ff_challenge_join cj ON f.friend_user_id = cj.user_id
                LEFT JOIN ff_user u ON f.friend_user_id = u.user_id
                WHERE f.user_id = " .$userId. " AND cj.challenge_id = " .$challengeId. "
                ORDER BY cj.create_time ASC
                LIMIT " .($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE');

        $list = M()->query($sql);

        return $list;

    }

}

